/**
 * Title: RoleController.java
 *
 * @author wujin
 * @date 2019年12月30日
 * @version 1.0
 */
package com.pm.controller.task;

import com.github.pagehelper.PageInfo;
import com.pm.common.Pages;
import com.pm.common.Response;
import com.pm.common.annotation.MyLog;
import com.pm.common.annotation.UserLoginToken;
import com.pm.common.exception.CommonException;
import com.pm.common.exception.ExpCodeState;
import com.pm.entity.po.task.SubtaskDeptTask;
import com.pm.entity.po.task.TaskSubtasks;
import com.pm.entity.vo.dept.DeptScore;
import com.pm.entity.vo.dept.DeptScoreQuarterVo;
import com.pm.entity.vo.dept.DeptTaskNumVo;
import com.pm.entity.vo.task.TaskVo;
import com.pm.service.task.SubtaskDeptTaskService;
import com.pm.service.task.TaskSubtasksService;
import com.pm.utils.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Title: RoleController
 *
 * @author wujin
 * @date 2019年12月30日
 */
@Api(tags = "目标任务分解")
@ApiModel
@UserLoginToken
@RestController
@RequestMapping("/deptTask")
public class SubtaskDeptTaskController {

    @Autowired
    private SubtaskDeptTaskService subtaskDeptTaskService;
    @Autowired
    private TaskSubtasksService distributionService;

    @ApiOperation("新增分解信息")
    @PostMapping("/add")
    @ResponseBody
    @MyLog(value = "新增指标任务分解信息")
    @Transactional(rollbackFor = CommonException.class)
    public Response<Integer> add(@RequestBody TaskSubtasks taskSubtasks) {
        Response<Integer> response = null;
        distributionService.update(taskSubtasks);
        SubtaskDeptTask ss = new SubtaskDeptTask();
        ss.setSubtasksId(taskSubtasks.getId());
        subtaskDeptTaskService.removeBatch(ss);
        Integer result = 0;
        if (taskSubtasks.getDeptTasks().size() == 0) {
            SubtaskDeptTask s = new SubtaskDeptTask();
            s.setTaskId(taskSubtasks.getTaskId());
            s.setSubtasksId(taskSubtasks.getId());
            s.setDepartmentId(taskSubtasks.getDepartmentId());
            s.setAllNumber(String.valueOf(taskSubtasks.getAllNumber()));
            if (taskSubtasks.getOneNumber() == null || 0.0 == taskSubtasks.getOneNumber()) {
                s.setOneQuarterNumber(null);
            } else {
                s.setOneQuarterNumber(String.valueOf(taskSubtasks.getOneNumber()));
            }
            if (taskSubtasks.getTwoNumber() == null || 0.0 == taskSubtasks.getTwoNumber()) {
                s.setTwoQuarterNumber(null);
            } else {
                s.setTwoQuarterNumber(String.valueOf(taskSubtasks.getTwoNumber()));
            }
            if (taskSubtasks.getThreeNumber() == null || 0.0 == taskSubtasks.getThreeNumber()) {
                s.setThreeQuarterNumber(null);
            } else {
                s.setThreeQuarterNumber(String.valueOf(taskSubtasks.getThreeNumber()));
            }
            result = subtaskDeptTaskService.add(s);
        }
        for (SubtaskDeptTask s : taskSubtasks.getDeptTasks()) {
            s.setTaskId(taskSubtasks.getTaskId());
            result += subtaskDeptTaskService.add(s);
        }
        if (result > 0) {
            response = new Response<>(ExpCodeState.update_data_success, result);
        } else {
            response = new Response<>(ExpCodeState.update_data_failed);
        }
        return response;
    }

    @ApiOperation("查询分解信息")
    @PostMapping("/loadDecomposeInfo")
    @ResponseBody
    public Response<TaskSubtasks> loadDecomposeInfo(@RequestBody TaskSubtasks taskSubtasks) {
        Response<TaskSubtasks> response = null;
        TaskSubtasks result = distributionService.load(taskSubtasks);
        SubtaskDeptTask subtaskDeptTask = new SubtaskDeptTask();
        subtaskDeptTask.setSubtasksId(taskSubtasks.getId());
        List<SubtaskDeptTask> resultList = subtaskDeptTaskService.getList(subtaskDeptTask);
        result.setDeptTasks(resultList);
        response = new Response<TaskSubtasks>(ExpCodeState.query_data_success, result);
        return response;
    }

    @ApiOperation("删除分解信息")
    @PostMapping("/remove")
    @ResponseBody
    @MyLog(value = "删除指标任务分配信息")
    public Response<Integer> remove(@RequestBody SubtaskDeptTask subtaskDeptTask) {
        Response<Integer> response = null;
        Integer result = subtaskDeptTaskService.remove(subtaskDeptTask);
        if (result > 0) {
            response = new Response<>(ExpCodeState.update_data_success, result);
        } else {
            response = new Response<>(ExpCodeState.update_data_failed);
        }
        return response;
    }

    @ApiOperation("添加备注")
    @PostMapping("/update")
    @ResponseBody
    @MyLog(value = "编辑指标任务分配信息")
    @Transactional(rollbackFor = CommonException.class)
    public Response<Integer> update(SubtaskDeptTask subtaskDeptTask) {
        Response<Integer> response = null;
        Integer result = subtaskDeptTaskService.update(subtaskDeptTask);
        if (result > 0) {
            response = new Response<>(ExpCodeState.update_data_success, result);
        } else {
            response = new Response<>(ExpCodeState.update_data_failed);
        }
        return response;
    }

    @ApiOperation("单个对象查询接口")
    @PostMapping("/load")
    @ResponseBody
    public Response<SubtaskDeptTask> load(SubtaskDeptTask subtaskDeptTask) {
        Response<SubtaskDeptTask> response = null;
        SubtaskDeptTask result = subtaskDeptTaskService.load(subtaskDeptTask);
        response = new Response<SubtaskDeptTask>(ExpCodeState.query_data_success, result);
        return response;
    }

    @ApiOperation("全部列表查询接口")
    @PostMapping("/getList")
    @ResponseBody
    @MyLog(value = "查询指标分配列表")
    public Response<List<SubtaskDeptTask>> getList(SubtaskDeptTask subtaskDeptTask) {
        List<SubtaskDeptTask> result = subtaskDeptTaskService.getList(subtaskDeptTask);
        Response<List<SubtaskDeptTask>> res = new Response<List<SubtaskDeptTask>>(ExpCodeState.query_data_success,
                result);
        return res;
    }


    @ApiOperation("分页查询接口")
    @PostMapping("/getListByPage/{pageNum}/{pageSize}")
    @ResponseBody
    @MyLog(value = "查询指标分配列表")
    public Response<PageInfo<SubtaskDeptTask>> getList(SubtaskDeptTask subtaskDeptTask, Pages page) {
        PageInfo<SubtaskDeptTask> result = subtaskDeptTaskService.getListByPage(subtaskDeptTask, page);
        Response<PageInfo<SubtaskDeptTask>> res = new Response<>(ExpCodeState.query_data_success, result);
        return res;
    }

    @ApiOperation("根据重点任务查询配合单位信息")
    @PostMapping("/getDeptList")
    @ResponseBody
    @MyLog(value = "根据重点任务查询配合单位信息")
    public Response<List<DeptScore>> getDeptList(@RequestBody SubtaskDeptTask subtaskDeptTask) {
        List<DeptScore> result = subtaskDeptTaskService.getDeptList(subtaskDeptTask);
        Response<List<DeptScore>> res = new Response<>(ExpCodeState.query_data_success, result);
        return res;
    }

    @ApiOperation("考核结果填报列表数据查询")
    @PostMapping("/getDeptScoreList")
    @ResponseBody
    @MyLog(value = "考核结果填报列表数据查询")
    public Response<List<DeptScore>> getDeptScoreList(@RequestBody SubtaskDeptTask subtaskDeptTask) {
        List<DeptScore> result = subtaskDeptTaskService.getDeptScoreList(subtaskDeptTask);
        Response<List<DeptScore>> res = new Response<>(ExpCodeState.query_data_success, result);
        return res;
    }

    @ApiOperation("根据重点任务查询季度配合单位信息")
    @PostMapping("/getDeptQuarter")
    @ResponseBody
    @MyLog(value = "根据重点任务查询季度配合单位信息")
    public Response<List<DeptScoreQuarterVo>> getDeptQuarter(DeptScoreQuarterVo deptScoreQuarterVo) {
        List<DeptScoreQuarterVo> result = subtaskDeptTaskService.getDeptQuarter(deptScoreQuarterVo);
        Response<List<DeptScoreQuarterVo>> res = new Response<List<DeptScoreQuarterVo>>(ExpCodeState.query_data_success, result);
        return res;
    }

    @ApiOperation("根据季度查询重点任务打分id集合")
    @PostMapping("/getDeptQuarterIds")
    @ResponseBody
    @MyLog(value = "根据重点任务查询季度配合单位信息")
    public Response<List<Integer>> getDeptQuarterIds(@RequestBody DeptScoreQuarterVo deptScoreQuarterVo) {
        List<Integer> result = subtaskDeptTaskService.getDeptQuarterIds(deptScoreQuarterVo);
        Response<List<Integer>> res = new Response<>(ExpCodeState.query_data_success, result);
        return res;
    }

    @ApiOperation("根据重点任务查询季度是否可以开始审核")
    @PostMapping("/getDeptQuarterExamine")
    @ResponseBody
    @MyLog(value = "根据重点任务查询季度是否可以开始审核")
    public Response<Boolean> getDeptQuarterExamine(@RequestBody DeptScoreQuarterVo deptScoreQuarterVo) {
        Boolean result = subtaskDeptTaskService.getDeptQuarterExamine(deptScoreQuarterVo);
        Response<Boolean> res = new Response<>(ExpCodeState.query_data_success, result);
        return res;
    }

    @ApiOperation("获取当前类型任务上一季度打分过的单位")
    @PostMapping("/getUpQuarterDept")
    @ResponseBody
    @MyLog(value = "获取当前类型任务上一季度打分过的单位")
    public Response<List<Map<String, String>>> getUpQuarterDept(DeptScoreQuarterVo deptScoreQuarterVo) {
        if (deptScoreQuarterVo.getTime() == null || deptScoreQuarterVo.getTime() <= 1
                || deptScoreQuarterVo.getTaskId() == null || deptScoreQuarterVo.getDepartmentTypeId() == null) {
            return new Response<>(ExpCodeState.param_exception, null);
        }
        deptScoreQuarterVo.setTime(deptScoreQuarterVo.getTime() - 1);
        List<Map<String, String>> result = subtaskDeptTaskService.getUpQuarterDept(deptScoreQuarterVo);
        Response<List<Map<String, String>>> res = new Response<>(ExpCodeState.query_data_success, result);
        return res;
    }

    @ApiOperation("查询单位任务数量")
    @PostMapping("/getDeptTaskNum")
    @ResponseBody
    @MyLog(value = "查询单位任务数量")
    public Response<List<Map<String, Object>>> getDeptTaskNum(@RequestBody DeptTaskNumVo deptTaskNumVo) {
        if (deptTaskNumVo.getYear() == null) {
            // 默认当前年
            deptTaskNumVo.setYear(DateUtil.getYear(new Date()));
        }
        List<Map<String, Object>> result = subtaskDeptTaskService.getDeptTaskNum(deptTaskNumVo);
        Response<List<Map<String, Object>>> res = new Response<>(ExpCodeState.query_data_success, result);
        return res;
    }

    @ApiOperation("查询单位类型任务数量")
    @PostMapping("/getDeptTypeTaskNum")
    @ResponseBody
    @MyLog(value = "查询单位类型任务数量")
    public Response<List<Map<String, Object>>> getDeptTypeTaskNum(@RequestBody TaskVo taskVo) {
        List<Map<String, Object>> result = subtaskDeptTaskService.getDeptTypeTaskNum(taskVo);
        Response<List<Map<String, Object>>> res = new Response<>(ExpCodeState.query_data_success, result);
        return res;
    }

    @ApiOperation("根据重点工作id查询单位")
    @PostMapping("/getDeptByTaskId")
    @ResponseBody
    @MyLog(value = "根据重点工作id查询单位")
    public Response<List<Map<String, Object>>> getDeptByTaskId(@RequestBody TaskVo taskVo) {
        List<Map<String, Object>> result = subtaskDeptTaskService.getDeptByTaskId(taskVo);
        Response<List<Map<String, Object>>> res = new Response<>(ExpCodeState.query_data_success, result);
        return res;
    }

}
